package cn.tudu.boot22.controller;

import cn.tudu.boot22.entity.Hero;
import cn.tudu.boot22.mapper.HeroMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class HeroController {
    @Autowired  //(required = false)   自动装配注解的作用:Spring框架结合Mybatis框架会自动将HeroMapper生成一个实现类和实现里面的方法
             //而且自动实例化该类 , required = false 告诉idea编译器此对象是非必要的
    HeroMapper mapper;
    @RequestMapping("/add")
    @ResponseBody
    public String add(Hero hero){
        System.out.println("hero = " + hero);
        //此方法相当于执行的JDBC代码
        mapper.insert(hero);
        return "添加完成";
    }
    @RequestMapping("/delete")
    @ResponseBody
    public String delete(String name){
         mapper.deleteByName(name);
        return "删除成功";
    }
    @RequestMapping("/update")
    @ResponseBody
    public String update(Hero hero){
        mapper.update(hero);
        return "修改成功";
    }
    @RequestMapping("/select")
    @ResponseBody
    public String select(){
        List<Hero> list = mapper.select();
        return list.toString();
    }
}
